package com.tt.lcd.qunyangGPRS.dao;

import android.content.Context;

import com.tt.lcd.qunyangGPRS.dao.renshen.XiaoqunyangDao;
import com.tt.lcd.qunyangGPRS.entity.DateShow;
import com.tt.lcd.qunyangGPRS.entity.JinSiData;
import com.tt.lcd.qunyangGPRS.entity.PigInfo;
import com.tt.lcd.qunyangGPRS.entity.XiaoQunYnag;
import com.tt.lcd.qunyangGPRS.entity.ZhanShow;
import com.tt.lcd.qunyangGPRS.utils.Register;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class DataManager {
	private Context mContext;
	public DataManager(Context context) {
		mContext=context;
	}

	public int getPigCount() {
		int c = 0;

		ArrayList<byte[]> list = new XiaoqunyangDao(mContext).select();

		for (int i = 0; i <list.size(); i++) {
			if(list.get(i).length!=0){
				c = c + list.get(i)[17];
			}
		}

		return c;
	}

	public int getZhanShu() {
		int c;
		c = new XiaoqunyangDao(mContext).selectAll().size();
		return c;
	}

	public List<HashMap<String, Object>> getPigInfo(String zhanhao,String date) {
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		ArrayList<byte[]> list = new XiaoqunyangDao(mContext).select();
		try {
			ArrayList<JinSiData> jinList = new ArrayList<JinSiData>();
			for (int i = 0; i < list.size(); i++) {
				byte[] b = list.get(i);
				for (int i1 = 18; i1 < b.length; i1 = i1 + 24) {
					byte[] jin = new byte[] { b[i1 + 0], b[i1 + 1], b[i1 + 2],
							b[i1 + 3], b[i1 + 4], b[i1 + 5], b[i1 + 6], b[i1 + 7],
							b[i1 + 8], b[i1 + 9], b[i1 + 10], b[i1 + 11],
							b[i1 + 12], b[i1 + 13], b[i1 + 14], b[i1 + 15],
							b[i1 + 16], b[i1 + 17], b[i1 + 18], b[i1 + 19],
							b[i1 + 20], b[i1 + 21], b[i1 + 22], b[i1 + 23] };
					jinList.add(new JinSiData(jin,""+b[15] + "-" + b[16]));

				}
			}
			// 从数据集合中取出指定日期的数据集合
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date da = sdf.parse(date);

			ArrayList<PigInfo> show = new ArrayList<PigInfo>();
			for (int i = 0; i < jinList.size(); i++) {
				if (jinList.get(i).getDate().equals(da)&&jinList.get(i).getZhanID().equals(zhanhao)) {
					JinSiData j = jinList.get(i);
					PigInfo p = new PigInfo();
					p.setPigid(j.getPigID());
					p.setJihua(j.getJihua());
					p.setJinsi(j.getJinsi());
					p.setWeiwan(j.getJihua()-j.getJinsi());
					show.add(p);
				}
			}

			for (int i = 0; i < show.size(); i++) {
				HashMap<String, Object> item = new HashMap<String, Object>();
				item.put("erbiao", show.get(i).getPigid());
				item.put("ycaisi", show.get(i).getJihua());
				item.put("yicaisi",show.get(i).getJinsi() );
				item.put("weiwan", show.get(i).getWeiwan());
				data.add(item);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return data;
	}

	public List<HashMap<String, Object>> getZhanInfo(String date) {
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		ArrayList<byte[]> list = new XiaoqunyangDao(mContext).select();

		try {

			ArrayList<JinSiData> jinList = new ArrayList<JinSiData>();
			for (int i = 0; i < list.size(); i++) {

				byte[] b = list.get(i);
				for (int i1 = 18; i1 < b.length; i1 = i1 + 24) {
					byte[] jin = new byte[] { b[i1 + 0], b[i1 + 1], b[i1 + 2],
							b[i1 + 3], b[i1 + 4], b[i1 + 5], b[i1 + 6], b[i1 + 7],
							b[i1 + 8], b[i1 + 9], b[i1 + 10], b[i1 + 11],
							b[i1 + 12], b[i1 + 13], b[i1 + 14], b[i1 + 15],
							b[i1 + 16], b[i1 + 17], b[i1 + 18], b[i1 + 19],
							b[i1 + 20], b[i1 + 21], b[i1 + 22], b[i1 + 23] };
					jinList.add(new JinSiData(jin,""+b[15] + "-" + b[16]));

				}
			}
			// 从数据集合中取出指定日期的数据集合
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date da = sdf.parse(date);

			ArrayList<ZhanShow> show = new ArrayList<ZhanShow>();
			for (int i = 0; i < jinList.size(); i++) {
				if (jinList.get(i).getDate().equals(da)) {
					boolean bl = true;
					for (int j = 0; j < show.size(); j++) {
						if (show.get(j).getZhanID().equals(jinList.get(i).getZhanID())) {
							show.get(j).setPigNunber(
									show.get(j).getPigNunber() + 1);
							bl = false;
							break;
						}
					}
					if (bl) {
						ZhanShow z = new ZhanShow();
						z.setZhanID(jinList.get(i).getZhanID());
						z.setPigNunber(1);
						show.add(z);
					}
				}
			}

			for (int i = 0; i < show.size(); i++) {
				HashMap<String, Object> item = new HashMap<String, Object>();
				item.put("id", "");
				item.put("zhanId", show.get(i).getZhanID());
				item.put("muzhu_shuliang", show.get(i).getPigNunber());
				data.add(item);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return data;
	}

	public List<HashMap<String, Object>> getDateInfo() throws ParseException {
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

		ArrayList<byte[]> list = new XiaoqunyangDao(mContext).select();


		ArrayList<JinSiData> jinList = new ArrayList<JinSiData>();
		for (int i = 0; i < list.size(); i++) {
			byte[] b = list.get(i);
			for (int i1 = 18; i1 < b.length; i1 = i1 + 24) {
				byte[] jin = new byte[] { b[i1 + 0], b[i1 + 1], b[i1 + 2],
						b[i1 + 3], b[i1 + 4], b[i1 + 5], b[i1 + 6], b[i1 + 7],
						b[i1 + 8], b[i1 + 9], b[i1 + 10], b[i1 + 11],
						b[i1 + 12], b[i1 + 13], b[i1 + 14], b[i1 + 15],
						b[i1 + 16], b[i1 + 17], b[i1 + 18], b[i1 + 19],
						b[i1 + 20], b[i1 + 21], b[i1 + 22], b[i1 + 23] };
				jinList.add(new JinSiData(jin,""+b[15] + "-" + b[16]));
			}
		}
		ArrayList<DateShow> show = new ArrayList<DateShow>();

//			ArrayList<Integer> zhan = new ArrayList<Integer>();
		for (int i = 0; i < jinList.size(); i++) {
			Date da = jinList.get(i).getDate();
			boolean bl = true;

			for (int j = 0; j < show.size(); j++) {
				if (show.get(j).getDate().equals(da)) {
					show.get(j).setPigs(show.get(j).getPigs() + 1);

					bl = false;
					break;
				}
			}
			if (bl) {
				DateShow ds = new DateShow();
				ds.setDate(da);
				ds.setPigs(1);
				ds.setZhanshu(1);
				show.add(ds);
			}
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = show.size()-1; i > -1; i--) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("muzhu", show.get(i).getPigs());


			item.put("date", sdf.format(show.get(i).getDate()));

			ArrayList<String> zz = new ArrayList<String>();
			for (int j = 0; j < jinList.size(); j++) {
				if(show.get(i).getDate().equals(jinList.get(j).getDate())){
					boolean bb =true;
					for (int j2 = 0; j2 < zz.size(); j2++) {
						if(zz.get(j2).equals(jinList.get(j).getZhanID())){
							bb = false;
							break;
						}

					}

					if(bb){
						zz.add(jinList.get(j).getZhanID());
					}
				}

			}
			item.put("shuliang", zz.size());
			data.add(item);
		}

		HashMap<String, Object> temp;
		for(int i=0;i<data.size()-1;i++){
			for (int j = 0; j < data.size()-i-1; j++) {
				Date d1 = sdf.parse(data.get(j).get("date").toString());
				Date d2 = sdf.parse(data.get(j+1).get("date").toString());
				if (d1.getTime() < d2.getTime()) {
					temp = data.get(j);
					data.set(j,data.get(j+1));
					data.set(j+1, temp);
				}
			}
		}
		//补齐有一段时间没更新的情况
		List<HashMap<String, Object>> data2 = new ArrayList<HashMap<String, Object>>();
		if(data.size()!=0&&!data.get(0).get("date").toString().equals(sdf.format(new Date()))){
			Date dangtian = new Date();
			Date qian = sdf.parse(data.get(0).get("date").toString());

			//算出相差天数
			long l1 = dangtian.getTime();
			long l2 = qian.getTime();
			int tian = (int) ((l1-l2)/1000/60/60/24);

			tian = tian-1;
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTime(dangtian);

			for (int j = 0; j < tian; j++) {
				//如果不是连续补齐空白天数
				calendar1.set(Calendar.DAY_OF_MONTH,calendar1.get(Calendar.DAY_OF_MONTH)-1);//让日期加1

				HashMap<String, Object> item = new HashMap<String, Object>();
				item.put("muzhu","数据未更新");
				item.put("date", sdf.format(calendar1.getTime()));
				item.put("shuliang", "数据未更新");
				data2.add(item);
			}
		}

		//补齐中间空白的天数
		for (int i = 0; i < data.size(); i++) {
			data2.add(data.get(i));

			if(i!=(data.size()-1)){  // 判断是不是最后一条数据，最后一条不处理
				String date1 = data.get(i).get("date").toString();
				String date2 = data.get(i+1).get("date").toString();
				Date d1 = sdf.parse(date1);
				Date d2 = sdf.parse(date2);

				Calendar calendar2=Calendar.getInstance();
				calendar2.setTime(d2);
				calendar2.set(Calendar.DAY_OF_MONTH,calendar2.get(Calendar.DAY_OF_MONTH)+1);//让日期加1
				String temp1 = sdf.format(calendar2.getTime());
				if(!temp1.equals(date1)){//判断两条记录日期是不是连续
					calendar2.set(Calendar.DAY_OF_MONTH,calendar2.get(Calendar.DAY_OF_MONTH)-1);//让日期还原

					//算出相差天数
					long l1 = d1.getTime();
					long l2 = d2.getTime();
					int tian = (int) ((l1-l2)/1000/60/60/24);

					tian = tian-1;
					Calendar calendar1 = Calendar.getInstance();
					calendar1.setTime(d1);
					for (int j = 0; j < tian; j++) {
						//如果不是连续补齐空白天数
						calendar1.set(Calendar.DAY_OF_MONTH,calendar1.get(Calendar.DAY_OF_MONTH)-1);//让日期加1

						HashMap<String, Object> item = new HashMap<String, Object>();
						item.put("muzhu",0);
						item.put("date", sdf.format(calendar1.getTime()));
						item.put("shuliang", 0);
						data2.add(item);
					}
				}
			}

		}

		return data2;
	}



	//指定饲喂站 当天采食不足的信息
	public List<HashMap<String, Object>> getNowdaitai(String zhanhao){
		List<HashMap<String, Object>> list1 = new ArrayList<HashMap<String,Object>>();
		ArrayList<byte[]> list = new ArrayList<byte[]>();
		ArrayList<XiaoQunYnag> list3 = new XiaoqunyangDao(mContext).selectAll();
		for (int i = 0; i < list3.size(); i++) {
			list.add(list3.get(i).getNowLog());
		}

		for(int i=0;i<list.size();i++){
			if (list.get(i).length>0) {
				String s = Register.getInt(list.get(i)[4]) + "-"
						+ Register.getInt(list.get(i)[5]);
				if (s.equals(zhanhao)) {
					for (int j = 0; j < Register.getInt(list.get(i)[6]); j++) {
						//耳标
						byte[] pid = new byte[15];
						for (int d = 0; d < pid.length; d++) {
							pid[d] = list.get(i)[j * 21 + 7 + d];
						}
						String erbiao = new String(pid);
						//采食量
						int caisi = 0;
						caisi = (Register.getInt(list.get(i)[j * 21 + 7 + 17]) << 8)
								+ Register.getInt(list.get(i)[j * 21 + 7 + 18]);
						//剩余量
						int shenyu = 0;
						shenyu = (Register.getInt(list.get(i)[j * 21 + 7 + 19]) << 8)
								+ Register.getInt(list.get(i)[j * 21 + 7 + 20]);
						//计划量
						int jihua = caisi + shenyu;
						HashMap<String, Object> item = new HashMap<String, Object>();
						item.put("erbiao", erbiao);
						item.put("ycaisi", "" + jihua);
						item.put("yicaisi", "" + caisi);
						item.put("weiwan", "" + shenyu);
						list1.add(item);
					}

					break;
				}
			}
		}

		return list1;
	}


}
